{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import glob\n",
    "import datetime\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.basemap import Basemap\n",
    "from matplotlib import rcParams\n",
    "import netCDF4\n",
    "import logging\n",
    "#from plot_AVISO_data import prepareDateStrings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Configuration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Logging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "logger = logging.getLogger()\n",
    "logger.setLevel(logging.INFO)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Plotting (should be ideally in `matplotlibrc` but seems not to work with virtualenv)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "rcParams.update({'font.size': 12, 'figure.dpi': 300, 'savefig.bbox': 'tight'})\n",
    "# rcParams.keys"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Input parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Files and directories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "databasedir = \"/home/ctroupin/Data/AVISO/regional-mediterranean/delayed-time/along-track/filtered/adt\"\n",
    "resultdir = \"/home/ctroupin/Projects/201704_DivaAltimetry/results/divand\"\n",
    "figdir = \"/home/ctroupin/Projects/201704_DivaAltimetry/figures/divand/\"\n",
    "resultfile = os.path.join(resultdir, \"ADT_20140515_20140604.nc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Domain and period of interest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "coordinates = [-6.75, 40.001, 30, 48.]\n",
    "year, month, day, interval = 2014, 5, 25, 10\n",
    "timescale = 5.        # temporal scale in days"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Interpolation grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lonmin, lonmax, latmin, latmax = -7., 40., 30., 48.\n",
    "dx, dy, dt = 0.25, 0.25, 1.\n",
    "lonr = np.arange(lonmin, lonmax + .1, dx)\n",
    "latr = np.arange(latmin, latmax +.1, dy)\n",
    "timer = np.arange(23510, 23529 + .1, dt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Map projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "m = Basemap(projection='merc',\n",
    "            llcrnrlon=coordinates[0], llcrnrlat=coordinates[2],\n",
    "            urcrnrlon=coordinates[1], urcrnrlat=coordinates[3],\n",
    "            lat_ts=0.5 * (coordinates[2] + coordinates[3]), resolution='i')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Make the plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Read netCDF file"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "File not too big so I read all its content at the same time.<br>\n",
    "If needed easy to read slice by slice in the loop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20, 73, 189)"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nc = netCDF4.Dataset(resultfile)\n",
    "field = nc.variables['ADT'][:]\n",
    "nc.close()\n",
    "field.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loop on the time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ctroupin/Software/PythonEnvs/Diva-python3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3413: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.\n",
      "  b = ax.ishold()\n",
      "/home/ctroupin/Software/PythonEnvs/Diva-python3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3422: MatplotlibDeprecationWarning: axes.hold is deprecated.\n",
      "    See the API Changes document (http://matplotlib.org/api/api_changes.html)\n",
      "    for more details.\n",
      "  ax.hold(b)\n",
      "/home/ctroupin/Software/PythonEnvs/Diva-python3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:1767: MatplotlibDeprecationWarning: The get_axis_bgcolor function was deprecated in version 2.0. Use get_facecolor instead.\n",
      "  axisbgc = ax.get_axis_bgcolor()\n"
     ]
    }
   ],
   "source": [
    "llonr, llatr = np.meshgrid(lonr, latr)\n",
    "# Loop on time\n",
    "for tt, ttime in enumerate(timer):\n",
    "    \n",
    "    fig = plt.figure()\n",
    "    m.pcolormesh(llonr, llatr, field[tt, :, :], latlon=True, vmin=-.2, vmax=.2)\n",
    "    # Add lines, coastline etc    \n",
    "    m.drawmeridians(np.arange(-8., round(coordinates[1]), 8), \n",
    "                    labels=[0, 0, 0, 1], linewidth=.2)\n",
    "    m.drawparallels(np.linspace(round(coordinates[2]), round(coordinates[3]), 5), \n",
    "                    labels=[1, 0, 0, 0], linewidth=.2)\n",
    "    m.drawcoastlines(linewidth=.2)\n",
    "    m.fillcontinents()\n",
    "    cbar = plt.colorbar(shrink=.6, extend='both')\n",
    "    cbar.set_label(\" m\", rotation=0)\n",
    "    \n",
    "    dateref = datetime.datetime(1950, 1, 1).toordinal()\n",
    "    datefield = datetime.datetime.fromordinal(int(timer[tt] + dateref))\n",
    "    figtitledate = datefield.strftime(\"%Y-%m-%d\")\n",
    "    fignamedate = datefield.strftime(\"%Y%m%d\")\n",
    "    \n",
    "    plt.title(figtitledate)\n",
    "    plt.savefig(os.path.join(figdir, \"ADT_divand_{0}\".format(fignamedate)), dpi=300)\n",
    "    #plt.show()\n",
    "    plt.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Diva-python3.6",
   "language": "python",
   "name": "diva-python3.6"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
